package com.jp.knowledge.my.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.jiayen.cache.FileCacheManager;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.my.adapter.UserVipAdapter;
import com.jp.knowledge.my.model.VipModel;
import com.jp.knowledge.util.ResultSubscriber;
import com.jp.knowledge.view.JpDiver;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by 528787067@qq.com on 2017/5/6.
 */

public class UserVipActivityOld extends SlidingActivity implements View.OnClickListener, ResultSubscriber.OnResultListener {

    @ViewInject(R.id.vip_buy)
    private TextView vipBuy;
    @ViewInject(R.id.vip_invite)
    private TextView vipInvite;
    @ViewInject(R.id.vip_rv)
    private RecyclerView vipRv;

    private List<VipModel> vipModels;
    private UserVipAdapter userVipAdapter;
    private String cachePath;
    private FileCacheManager cacheManager;

    @Override
    protected int getResourceId() {
        return R.layout.activity_user_vip_old;
    }

    @Override
    protected void init() {
        topName.setText("VIP会员介绍");
        rightIcon.setVisibility(View.GONE);
        leftIcon.setImageResource(R.mipmap.arrow_white_left);
        leftIcon.setOnClickListener(this);
        vipBuy.setOnClickListener(this);
        vipInvite.setOnClickListener(this);
        vipRv.setHasFixedSize(true);
        vipRv.setLayoutManager(new LinearLayoutManager(mContext));

        vipModels = new ArrayList<>();
        userVipAdapter = new UserVipAdapter(mContext, vipModels);
        vipRv.addItemDecoration(new JpDiver(mContext));
        vipRv.setAdapter(userVipAdapter);

        cachePath = mContext.getCacheDir() + "/userVip.dat";
        cacheManager = new FileCacheManager();

        readCache();
    }

    private void readCache(){
        Observable.create(new Observable.OnSubscribe<List<VipModel>>() {
            @Override
            public void call(Subscriber<? super List<VipModel>> subscriber) {
                List<VipModel> data = (List<VipModel>) cacheManager.readObject(cachePath);
                subscriber.onNext(data);
            }
        }).subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Subscriber<List<VipModel>>() {
            @Override
            public void onCompleted() {
                getData();
            }
            @Override
            public void onError(Throwable e) {
                onCompleted();
            }
            @Override
            public void onNext(List<VipModel> data) {
                if(data != null) {
                    vipModels = data;
                    userVipAdapter.updateAdapter(vipModels);
                }
                onCompleted();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.icon_left:
                finish();
                break;
            case R.id.vip_buy:
                startActivity(new Intent(mContext, VipBuyActivity.class).putExtra("vipLevel", 1));
                break;
            case R.id.vip_invite:
                startActivity(new Intent(mContext, InviteActivity.class));
                break;
        }
    }

    private void getData(){
        JsonObject param = new JsonObject();
        param.addProperty("type", 0);
        HttpHelper.getInstance(mContext).userPrivilege(param, 0x01, this);
    }

    @Override
    public void onStart(int requestType) {}
    @Override
    public void onCompleted(int requestType) {}
    @Override
    public void onError(int requestType) {
        onCompleted(requestType);
    }
    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode() != 0){
            ToasUtil.toast(mContext, data.getMessage());
            onError(requestType);
        } else{
            vipModels = data.getList(VipModel.class);
            userVipAdapter.updateAdapter(vipModels);

            new Thread(new Runnable() {
                @Override
                public void run() {
                    cacheManager.wirteObject(vipModels, cachePath);
                }
            }).start();
        }
    }
}
